'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 领取累计充值活动奖励
 * @author agl
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var payConf = require('../../../../config/payConf.js');
var genRewardService = require('../../../../services/genRewardService');
var rewardService = require('../../../../services/rewardService');
var sysMsgType = require('../../../../consts/sysMsgType.js');
var sysMsgConf = require('../../../../config/sysMsgConf.js');
var marqueeConf = require('../../../../config/marqueeConf.js');
var soldierConf = require('../../../../config/soldierConf.js');
var chatMgr = require('../../../../mgr/chatMgr.js');
var chatType = require('../../../../consts/chatType.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    // 累计充值钻石数
    var price = msg.price;

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.series(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'build', 'soldier', 'bag', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var total_pay = role_model.total_pay;
                if (price > total_pay) {
                    return next(null, {
                        code: Code.PAY_NOT_ENOUGH_PAY
                    });
                }
                var pay_conf = payConf.getTotalPayConf(price);
                if (!pay_conf) {
                    // 分档无效
                    return next(null, {
                        code: Code.PAY_INVALID_REWARD_GRADE
                    });
                }

                // 添加到已领取奖励列表中
                if (!role_model.addToTotalPayPicked(price)) {
                    //已领取过对应奖励
                    return next(null, {
                        code: Code.PAY_REWARD_ALREADY_PICKED
                    });
                }

                // 发放奖励
                reward_list = genRewardService.generateRewardList(pay_conf);

                // 发放广播
                var count_pay = sysMsgConf.getMsgConf(sysMsgType.COUNTPAY);
                _.some(count_pay, function (count_pay_item) {
                    if (price === count_pay_item.value) {
                        var count_pay_content = count_pay_item.format;
                        var role_name = role_model.name;
                        var count_pay_content_msg = count_pay_content.replace(/name/, role_name);
                        var name = '系统';
                        var count_pay_param = {
                            type: chatType.SYSTEM,
                            name: name,
                            msg: count_pay_content_msg,
                            time: moment().unix()
                        };
                        _chatMsg(count_pay_param);
                        return true;
                    }
                });

                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_PAY, operationType.OPR_ACT_PAY_REWARD_TOTAL_PAY);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};

var _chatMsg = function (param) {
    chatMgr.sendWorld(param, function (err, fails) {
        if (err) {
            console.error('send message to world error: %j, fail ids: %j', err, fails);
            return;
        }
    });
};
